<template>
  <div class="top-box">
    <div style="cursor: pointer" class="fs-6" @click="router.push('/message')">&lt;</div>
    <div class="fs-6" v-if="messageInfo.mid">{{ messageInfo.title }}</div>
    <div>
      <HuUserInfo></HuUserInfo>
    </div>
  </div>

  <div class="message">
    <div v-if="messageInfo.mid">
      <div class="top-box1">
        <div>
          {{ messageInfo.user.info.nickname || messageInfo.user.user.username }}
          {{ Tools.formatDate(messageInfo.lastupdate, 'yyyy-MM-dd') }}
          浏览量：{{ messageInfo.hits }} 点赞量：{{ messageInfo.praise }}
        </div>
        <div>
          <NButton @click="jubaoAdd" type="error">举报</NButton>
        </div>
      </div>
    </div>

    <div v-else>
      <NEmpty description="留言信息获取中。。。"></NEmpty>
    </div>
    <div v-html="messageInfo.info"></div>
  </div>

  <NDivider>评论</NDivider>

  <!-- 切换排序 -->
  <div class="pl-20 pr-20" style="width: 50vw; margin: 0px auto">
    <NTabs @update-value="messageOrder">
      <NTabPane name="1" tab="发帖时间"></NTabPane>
      <NTabPane name="2" tab="点赞数"></NTabPane>
    </NTabs>
  </div>

  <div v-if="list.length == 0">
    <NEmpty description="暂无评论"></NEmpty>
    <div class="tc">
      <NButton @click="showAdd" type="primary">抢先发布</NButton>
    </div>
  </div>
  <div v-else>
    <div class="replies">
      <div v-for="d in list" :key="d.mid">
        <div class="content">
          <div>
            <NAvatar round :src="Api.userlogo(d.mid)"></NAvatar>
          </div>
          <div>
            <div>
              {{ d.user.info.nickname || d.user.user.username }}
              {{ Tools.formatDate(d.lastupdate) }}
              {{ d.exInfo.praised }}-
              <NIcon size="20" @click="praiseReply(d)" :class="{ active: d.exInfo.praised }">
                <svg
                  t="1745979472189"
                  class="icon"
                  viewBox="0 0 1024 1024"
                  version="1.1"
                  xmlns="http://www.w3.org/2000/svg"
                  p-id="4953"
                  width="256"
                  height="256"
                >
                  <path
                    d="M885.9 533.7c16.8-22.2 26.1-49.4 26.1-77.7 0-44.9-25.1-87.4-65.5-111.1a67.67 67.67 0 0 0-34.3-9.3H572.4l6-122.9c1.4-29.7-9.1-57.9-29.5-79.4-20.5-21.5-48.1-33.4-77.9-33.4-52 0-98 35-111.8 85.1l-85.9 311H144c-17.7 0-32 14.3-32 32v364c0 17.7 14.3 32 32 32h601.3c9.2 0 18.2-1.8 26.5-5.4 47.6-20.3 78.3-66.8 78.3-118.4 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7-0.2-12.6-2-25.1-5.6-37.1zM184 852V568h81v284h-81z m636.4-353l-21.9 19 13.9 25.4c4.6 8.4 6.9 17.6 6.9 27.3 0 16.5-7.2 32.2-19.6 43l-21.9 19 13.9 25.4c4.6 8.4 6.9 17.6 6.9 27.3 0 16.5-7.2 32.2-19.6 43l-21.9 19 13.9 25.4c4.6 8.4 6.9 17.6 6.9 27.3 0 22.4-13.2 42.6-33.6 51.8H329V564.8l99.5-360.5c5.2-18.9 22.5-32.2 42.2-32.3 7.6 0 15.1 2.2 21.1 6.7 9.9 7.4 15.2 18.6 14.6 30.5l-9.6 198.4h314.4C829 418.5 840 436.9 840 456c0 16.5-7.2 32.1-19.6 43z"
                    p-id="4954"
                  ></path>
                </svg>
              </NIcon>
              <span>({{ d.praise }})</span>
              <div>{{ d.info }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="tc p-2">
      <NButton @click="showAdd" type="primary">发布评论</NButton>
    </div>
  </div>

  <div class="p-4 tc" v-if="loading">
    <!-- <NButton @click="loadmore">加载更多...</NButton> -->
    <span> <NSpin size="small"></NSpin>正在加载中... </span>
  </div>
  <div class="p-4 tc" v-if="page.pageNumber >= page.pageCount && !loading">我是有底线的</div>

  <NBackTop></NBackTop>

  <!-- 发布评论的对话框 -->
  <div>
    <NModal preset="dialog" v-model:show="visible" title="发布评论">
      <div>
        <NInput type="textarea" v-model:value="content" placeholder="评论内容"></NInput>
      </div>
      <template #action>
        <NButton type="primary" @click="addReply">发布</NButton>
      </template>
    </NModal>
  </div>

  <!-- 举报的对话框 -->
  <div>
    <NModal preset="dialog" v-model:show="aisible" title="举报">
      <div>
        <NInput type="textarea" v-model:value="content1" placeholder="举报内容"></NInput>
      </div>
      <template #action>
        <NButton type="primary" @click="addjubao">发布</NButton>
      </template>
    </NModal>
  </div>
</template>

<script setup lang="ts">
import type { TbMessage, TbMessageReply } from '@/types/Message'
import type { BaseDataResult, BaseResult, PageBean, PageVO } from '@/types/BaseResult'
import { Api } from '@/types/Api'
import { useRoute, useRouter } from 'vue-router'
import { Tools } from '@/types/Tools'
import { ref, onMounted } from 'vue'
import {
  NAvatar,
  NButton,
  NDivider,
  NEmpty,
  NInput,
  NModal,
  NTabPane,
  NTabs,
  useMessage,
  NBackTop,
  NIcon,
} from 'naive-ui'
import HuUserInfo from '@/components/HuUserInfo.vue'
const message = useMessage()

const route = useRoute()
const router = useRouter()
const mid = route.params.id
console.log('留言板id：', mid)
const loading = ref(false)
const orderBy = ref(1)

//# region
const messageOrder = (order: number) => {
  orderBy.value = order
  requery()
}

const requery = () => {
  page.value.pageNumber = 1
  list.value = []
  query()
}
//#endregion

//# region 贴吧详情
const messageInfo = ref({} as TbMessage)

const query = () => {
  loading.value = true
  Api.get(`/message/${mid}`, {}, (data: BaseDataResult<TbMessage>) => {
    loading.value = false
    if (data.success) {
      messageInfo.value = data.data
      queryReply()
      return
    }
    message.error(data.message)
  })
}
//# endregion

//# region 贴吧评论
const page = ref({ pageNumber: 1, pageSize: 10 } as PageBean)
const list = ref([] as Array<TbMessageReply>)

const queryReply = () => {
  loading.value = true
  Api.get(
    `/message/reply/${mid}/${page.value.pageSize}/${page.value.pageNumber}/${orderBy.value}`,
    {},
    (data: BaseDataResult<PageVO<TbMessageReply>>) => {
      loading.value = false
      if (data.success) {
        list.value = data.data.list
        page.value = data.data.pageBean
        return
      }
      message.error(data.message)
    },
  )
}

const praiseReply = (info: TbMessageReply) => {
  Api.post(`/reply/praise/${info.mrid}`, {}, (data: BaseDataResult<number>) => {
    if (data.success) {
      info.exInfo.praised = !info.exInfo.praised
      return
    }
    message.error(data.message)
    console.log(data)
  })
}
//# endregion

//# region 发布评论
const visible = ref(false)
const content = ref('')
const showAdd = () => {
  content.value = ''
  visible.value = true
}

const addReply = () => {
  loading.value = true
  Api.post('/reply', { mid: mid, info: content.value }, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      visible.value = false
      message.success(data.message)
    } else {
      message.error(data.message)
    }
  })
}
//# endregion

//  #region  举报帖子
const aisible = ref(false)
const content1 = ref('')

const jubaoAdd = () => {
  content1.value = ''
  aisible.value = true
}

const addjubao = () => {
  loading.value = true
  Api.post('/report/message', { mid: mid, info: content1.value }, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      aisible.value = false
      message.success(data.message)
    } else {
      message.error(data.message)
    }
  })
}
// #endregion

onMounted(() => {
  query()
})
</script>

<style scoped lang="scss">
// .border-box {
//   border: 1px solid #f00b0b;
//   padding: 0.5rem;
// }

.top-box1 {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.top-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  position: sticky;
  top: 0px;
  z-index: 10;
  background-color: var(--body-color);
  padding: 1rem;
}

.message {
  width: 50vw;
  margin: 0px auto;
  :deep(p) {
    font-size: 1.2rem;
    text-indent: 2em;
  }

  > div:first-of-type {
    font-size: 1.2rem;
  }
}

.replies {
  width: 50vw;
  margin: 0px auto;
  > div {
    margin-bottom: 0.5rem;
  }
  .content {
    display: flex;
    align-items: flex-start;
    > div:last-of-type {
      flex: 1;
      margin-left: 0.5rem;
    }
    .active {
      color: #ff0000;
    }
  }
}
</style>
